package com.ruoyi.web.controller.haircut;

import java.util.Date;
import java.util.List;

import com.ruoyi.domain.VipDetail;
import com.ruoyi.domain.VipDetails;
import com.ruoyi.service.IVipDetailService;
import com.ruoyi.service.IVipDetailsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.VipHaircut;
import com.ruoyi.service.IVipHaircutService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 会员管理Controller
 *
 * @author ruoyi
 * @date 2020-08-09
 */
@Controller
@RequestMapping("/ruoyi/haircut")
public class VipHaircutController extends BaseController {
    private String prefix = "ruoyi/haircut";

    @Autowired
    private IVipHaircutService vipHaircutService;
    @Resource
    private IVipDetailService vipDetailService;
    @Resource
    private IVipDetailsService vipDetailsService;

    @RequiresPermissions("ruoyi:haircut:view")
    @GetMapping()
    public String haircut() {
        return prefix + "/haircut";
    }

    /**
     * 查询会员管理列表
     */
    @RequiresPermissions("ruoyi:haircut:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VipHaircut vipHaircut) {
        startPage();
        List<VipHaircut> list = vipHaircutService.selectVipHaircutList(vipHaircut);
        return getDataTable(list);
    }

    /**
     * 导出会员管理列表
     */
    @RequiresPermissions("ruoyi:haircut:export")
    @Log(title = "会员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VipHaircut vipHaircut) {
        List<VipHaircut> list = vipHaircutService.selectVipHaircutList(vipHaircut);
        ExcelUtil<VipHaircut> util = new ExcelUtil<VipHaircut>(VipHaircut.class);
        return util.exportExcel(list, "haircut");
    }

    /**
     * 新增会员管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存会员管理
     */
    @RequiresPermissions("ruoyi:haircut:add")
    @Log(title = "会员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VipHaircut vipHaircut) {
        return toAjax(vipHaircutService.insertVipHaircut(vipHaircut));
    }

    /**
     * 修改会员管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        VipHaircut vipHaircut = vipHaircutService.selectVipHaircutById(id);
        mmap.put("vipHaircut", vipHaircut);
        return prefix + "/edit";
    }

    /**
     * 修改保存会员管理
     */
    @RequiresPermissions("ruoyi:haircut:edit")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VipHaircut vipHaircut) {
        return toAjax(vipHaircutService.updateVipHaircut(vipHaircut));
    }

    /**
     * 删除会员管理
     */
    @RequiresPermissions("ruoyi:haircut:remove")
    @Log(title = "会员管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(vipHaircutService.deleteVipHaircutByIds(ids));
    }


    /**
     * 充值
     */
    @GetMapping("/recharge/{id}")
    public String recharge(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/recharge";
    }

    /**
     * 充值保存
     */
    @RequiresPermissions("ruoyi:haircut:recharge")
    @Log(title = "充值", businessType = BusinessType.INSERT)
    @PostMapping("/recharge/{id}")
    @ResponseBody
    public AjaxResult rechargeSave(@PathVariable("id") Long id, VipHaircut vipHaircut) {
        //通过id查询当前用户的余额
        Long selectmoney = vipHaircutService.selectmoney(id);
        //获取输入的充值金额
        Long vipMoney = vipHaircut.getVipMoney();
        //更新的金额
        Long nowmoney = selectmoney + vipMoney;
        VipDetails vipDetails = new VipDetails();
        vipDetails.setId(id);
        vipDetails.setVipMoney(vipMoney);
        vipDetails.setVipTime(new Date());
        vipDetailsService.insertVipDetails(vipDetails);
        return toAjax(vipHaircutService.updatemoney(nowmoney, id));
    }


    /**
     * 消费
     */
    @GetMapping("/consume/{id}")
    public String consume(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/consume";
    }

    /**
     * 消费保存
     */
    @RequiresPermissions("ruoyi:haircut:recharge")
    @Log(title = "消费", businessType = BusinessType.INSERT)
    @PostMapping("/consume/{id}")
    @ResponseBody
    public AjaxResult consumeSave(@PathVariable("id") Long id, VipHaircut vipHaircut) {
        //通过id查询当前用户的余额
        Long selectmoney = vipHaircutService.selectmoney(id);
        //获取输入的消费金额
        Long vipMoney = vipHaircut.getVipMoney();
        if (vipMoney > selectmoney) {
            return AjaxResult.error("用户余额不足,请充值");
        } else {
            //更新的金额
            Long nowmoney = selectmoney - vipMoney;
            //向消费记录表插入数据
            VipDetail vipDetail = new VipDetail();
            //获取用户的id
            vipDetail.setId(id);
            //获取输入的消费金额
            vipDetail.setVipMoney(vipMoney);
            //获取输入的时间
            vipDetail.setVipTime(new Date());
            vipDetailService.insertVipDetail(vipDetail);
            return toAjax(vipHaircutService.updatemoney(nowmoney, id));
        }

    }
}
